package com.gusti.admincasa.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.gusti.admincasa.dao.IUserDao;
import com.gusti.admincasa.model.User;
import com.gusti.admincasa.util.Debug;

/**
 * 
 * @author gusti
 *
 */
@Repository
public class UserDao extends ProjectDao<User> implements IUserDao {

	public User findUser( String name ) {
		User user = new User();

		Query query = null;
		try {
			query = em.createQuery( "FROM User as usr WHERE usr.name = :name" );
			query.setParameter( "name", name );
			user = (User)query.getSingleResult();
		} catch ( Exception e ) {
			Debug.error( e );
		}

		return user;
	}

	@SuppressWarnings("unchecked")
	public List<User> listUsers() {
		List<User> users = new ArrayList<User>();

		Query query = null;
		try {
			query = em.createQuery( "FROM User as usr ORDER BY usr.name" );
			users = (List<User>)query.getResultList();
		} catch ( Exception e ) {
			Debug.error( e );
		}

		return users;
	}

}
